package com.example.demo.controller;

import com.example.demo.domain.Category;
import com.example.demo.req.CategoryQueryReq;
import com.example.demo.req.CategorySaveReq;
import com.example.demo.resp.CommonResp;
import com.example.demo.resp.PageResp;
import com.example.demo.services.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController //这是返回接口
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @GetMapping("/list")
    public CommonResp<PageResp<Category>> list(@Valid CategoryQueryReq req){
        CommonResp<PageResp<Category>> resp= new CommonResp<>();
        PageResp<Category> list = categoryService.list(req);
        resp.setData(list);
        return  resp;
    }

    @GetMapping("/all")
    public CommonResp<List<Category>> all(@Valid CategoryQueryReq req){
        CommonResp<List<Category>> resp= new CommonResp<>();
        List<Category> list = categoryService.all(req);
        resp.setData(list);
        return  resp;
    }

    @PostMapping("/save")
    public CommonResp<PageResp<Category>> save(@Valid @RequestBody CategorySaveReq req){
        CommonResp resp= new CommonResp<>();
        categoryService.save(req);
        return  resp;
    }

    @GetMapping("/delete")
    public CommonResp<PageResp<Category>> delete(CategorySaveReq req){
        CommonResp resp= new CommonResp<>();
        categoryService.delete(req);
        return  resp;
    }
}
